package com.young;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.convert.DataSizeUnit;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;

//打jar包需要继承SpringBootServletInitializer
@SpringBootApplication
public class Chapter6Application extends SpringBootServletInitializer {

	public static void main(String[] args) {
		SpringApplication.run(Chapter6Application.class, args);
	}

	@Override//为了打包springboot项目
	protected SpringApplicationBuilder configure(
			SpringApplicationBuilder builder) {
		return builder.sources(this.getClass());
	}

	@RestController
	class HelloController {
		@PostMapping("/user")
		public UserDto user(@RequestBody UserDto userDto) throws Exception {
			return userDto;
		}
	}

	/*
	* 这三个注解需要在pom中引入
	* <dependency>
			<groupId>org.projectlombok</groupId>
			<artifactId>lombok</artifactId>
			<version>1.16.20</version>
		</dependency>
		官网 https://projectlombok.org/features/all
		其中@Data用来自动生成@ToString, @EqualsAndHashCode, @Getter,@Setter
		其他注解可顾名思义
	* */
	@Data
	@NoArgsConstructor
	@AllArgsConstructor
	static class UserDto {
		private String userName;
		private LocalDate birthday;
	}

	//增加json提供的针对Java8LocalDate的序列化模块，禁用对日期以时间戳方式输出的特性
	//可通过POSTMAN模拟
	//用来修复Spring Boot和Feign中使用Java 8时间日期API（LocalDate等）的序列化问题
	//但此问题在4.0中已修复，此处只是为了演示旧版解决办法
	@Bean
	public ObjectMapper serializingObjectMapper() {
		ObjectMapper objectMapper = new ObjectMapper();
		objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
		objectMapper.registerModule(new JavaTimeModule());
		return objectMapper;
	}
}
